﻿using DevExpress.XtraSplashScreen;
using DevExpress.XtraTab;
using DevExpress.XtraTab.ViewInfo;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Deployment.Application;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using YDWin.CDK;
using YDWin.Customer;
using YDWin.Deying;
using YDWin.Embryo;
using YDWin.Employee;
using YDWin.Factory; 
using YDWin.Handle;
using YDWin.OrderReport;
using YDWin.Password;
using YDWin.Printing;
using YDWin.Role;
using YDWin.Sading;
using YDWin.Sending;
using YDWin.Spec;
using YDWin.SystemParameter;

namespace YDWin
{
    public partial class MainForm : Form
    {
        bool isEnd = false;
        string VerificationStr;
        public MainForm()
        { 
            InitializeComponent();
      
            if (!cdk.VerificationCDK(out VerificationStr))
            {
                isEnd = true;
            }
            barStaticItem2.Caption = VerificationStr;
            barStaticItem3.Caption = "操作员 : " + Global.User.UserName;


            barStaticItem6.Caption = GetVersiion();

            //设置gridcontrol默认字体
            DevExpress.Utils.AppearanceObject.DefaultFont = new System.Drawing.Font("宋体", 9);
        }

        private static string GetVersiion()
        {
            var version = "";
            if (ApplicationDeployment.IsNetworkDeployed)//是否已连接 
            {
                ApplicationDeployment currDeployment = ApplicationDeployment.CurrentDeployment;
                DateTime lastCheck = currDeployment.TimeOfLastUpdateCheck.ToLocalTime();
                var lastDay = lastCheck.Date.ToShortDateString();
                var lastTime = lastCheck.TimeOfDay.ToString();
                var majorVersion = currDeployment.CurrentVersion.Major.ToString();//主版本
                var minorVersion = currDeployment.CurrentVersion.Minor.ToString(); //次版本 
                var revisionVersion = currDeployment.CurrentVersion.Revision.ToString();//内部版本号
                var blildVersion = currDeployment.CurrentVersion.Build.ToString();//修订号

                version = majorVersion + "." + minorVersion + "." + blildVersion + "." + revisionVersion;
            }
            return version;
        }



        /// <summary>
        /// 订单管理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bbiOrder_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            //frmChild = new OrderForm(); 
            //openWindow();
        }


        #region 子窗体
        private Form frmChild = null;
        private void openWindow()
        {
            XtraTabPage page = new XtraTabPage();

            if (frmChild != null)
            {
                frmChild.Visible = true;
                frmChild.Dock = DockStyle.Fill;
                frmChild.FormBorderStyle = FormBorderStyle.None;
                frmChild.TopLevel = false;
            }

            page.Controls.Add(frmChild);
            page.Text = frmChild.Text;

            foreach (XtraTabPage xtp in main.TabPages)
            {
                if (xtp.Text.Equals(frmChild.Text))
                {
                    main.SelectedTabPage = xtp;
                    return;
                }
            }

            main.TabPages.Add(page);
            main.SelectedTabPage = page;

            //设置关闭事件 

            main.CloseButtonClick += new EventHandler(XtraTabControl1CloseButtonClick);
        }

        //在程序关闭之后 要释放窗体等资源 
        void XtraTabControl1CloseButtonClick(object sender, EventArgs e)
        {
            ClosePageButtonEventArgs a = (ClosePageButtonEventArgs)e;
            string tabpagename = a.Page.Text;
            foreach (Control xtp in main.TabPages)
            {
                if (xtp.Text == tabpagename)
                {
                    xtp.Dispose();
                    return;
                }
            }
        }
        #endregion



        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.Dispose();
            this.Close();

        }
         

        private void MainForm_Load(object sender, EventArgs e)
        {
            if(isEnd)
            {
                //if (MessageBox.Show("系统授权到期,请联系软件供应商,手机号:13757519621") == DialogResult.OK)
                //{
                //    this.Dispose();
                //    this.Close();
                //}

                if(VerificationStr.Equals("请保持网络畅通"))
                {
                    if (MessageBox.Show("请保持网络畅通,联网之后重试") == DialogResult.OK)
                    {
                        this.Dispose();
                        this.Close();
                    }
                }

            
                LockForm lf = new LockForm();
                lf.ShowDialog();
                return;
            }
            //如果是管理员,则显示员工维护
            //if(Global.User.UserCode.Contains("admin"))
            //{
            //    barButtonItem6.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
            //}
            //else
            //{
            //    barButtonItem6.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            //}

            checkPermission();

        }

        private void checkPermission()
        {
            this.ribbonPage2.Visible = false;
            this.ribbonPage4.Visible = false;

            this.barButtonItem9.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            this.barButtonItem10.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            this.barButtonItem11.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            this.barButtonItem12.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            this.barButtonItem14.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            this.barButtonItem15.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            this.barButtonItem16.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;

            this.barButtonItem4.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            this.barButtonItem5.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            this.barButtonItem2.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            this.barButtonItem6.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            this.ansh.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            this.barButtonItem13.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;

            //根据权限判断显示
            var userPerList = Global.UserPerList;
            if (userPerList.Count > 0)
            {
                foreach (var per in userPerList)
                {
                    if (per.PermissionCode.Equals("01") && per.IsChecked == true)
                    {
                        this.ribbonPage2.Visible = true;
                    } 
                    if (per.PermissionCode.Equals("0101") && per.IsChecked == true)
                    {
                        this.barButtonItem9.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                    }
                    if (per.PermissionCode.Equals("0102") && per.IsChecked == true)
                    {
                        this.barButtonItem10.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                    }
                    if (per.PermissionCode.Equals("0103") && per.IsChecked == true)
                    {
                        this.barButtonItem11.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                    }
                    if (per.PermissionCode.Equals("0104") && per.IsChecked == true)
                    {
                        this.barButtonItem12.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                    }
                    if (per.PermissionCode.Equals("0105") && per.IsChecked == true)
                    {
                        this.barButtonItem14.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                    }
                    if (per.PermissionCode.Equals("0106") && per.IsChecked == true)
                    {
                        this.barButtonItem15.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                    }
                    if (per.PermissionCode.Equals("0107") && per.IsChecked == true)
                    {
                        this.barButtonItem16.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                    }

                    if (per.PermissionCode.Equals("02") && per.IsChecked == true)
                    {
                        this.ribbonPage4.Visible = true;
                    }
                    if (per.PermissionCode.Equals("0201") && per.IsChecked == true)
                    {
                        this.barButtonItem4.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                    }
                    if (per.PermissionCode.Equals("0202") && per.IsChecked == true)
                    {
                        this.barButtonItem5.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                    }
                    if (per.PermissionCode.Equals("0203") && per.IsChecked == true)
                    {
                        this.barButtonItem2.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                    }
                    if (per.PermissionCode.Equals("0204") && per.IsChecked == true)
                    {
                        this.barButtonItem6.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                    }
                    if (per.PermissionCode.Equals("0205") && per.IsChecked == true)
                    {
                        this.ansh.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                    }
                    if (per.PermissionCode.Equals("0206") && per.IsChecked == true)
                    {
                        this.barButtonItem13.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                    }

                }
            }
        }

        /// <summary>
        /// CDK
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem1_ItemClick_1(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            CDKForm cf = new CDKForm();
            cf.ShowDialog();
        }

 
        /// <summary>
        /// 联系方式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ContactForm cf = new ContactForm();
            cf.ShowDialog();
        }

        /// <summary>
        /// 厂家维护
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem4_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmChild = new FactoryForm();
            openWindow();
        }
        /// <summary>
        /// 规格维护
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem5_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmChild = new SpecForm();
            openWindow();
        }

        /// <summary>
        /// 员工维护
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem6_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmChild = new EmployeeForm();
            openWindow();
        }

        /// <summary>
        /// 客户维护
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmChild = new CustomerForm();
            openWindow();
        }

        /// <summary>
        /// 切换登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem7_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            //Application.Exit();
            Application.ExitThread();
            Thread thtmp = new Thread(new ParameterizedThreadStart(run));
            object appName = Application.ExecutablePath;
            Thread.Sleep(1);
            thtmp.Start(appName);
        }
        private void run(Object obj)
        {
            Process ps = new Process();
            ps.StartInfo.FileName = obj.ToString();
            ps.Start();
        }

        /// <summary>
        /// 密码修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem8_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            EditPasswordForm epf = new EditPasswordForm();
            epf.ShowDialog();
        }

        /// <summary>
        /// 胚布管理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem9_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmChild = new EmbryoForm();
            openWindow(); 
        }

        private void barButtonItem10_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmChild = new SadingForm();
            openWindow();
        }

        private void barButtonItem12_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmChild = new DeyingForm();
            openWindow();
        }

        private void barButtonItem11_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmChild = new PrintingForm();
            openWindow();
        }

 

        private void barButtonItem14_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmChild = new HandleForm();
            openWindow();

        }

        private void barButtonItem15_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmChild = new SendingForm();
            openWindow();
        }

        private void barButtonItem16_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmChild = new OrderReportForm();
            openWindow();
        }

        private void ansh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmChild = new SystemParameterForm();
            openWindow();
        }

        private void barButtonItem13_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmChild = new RoleForm();
            openWindow();
        }
    }
}
